﻿using Lazy.Captcha.Core;
using YiShop.Framework.Constants;

namespace YiShop.System.Controllers
{
    [ApiController]
    public class SysLoginController : ControllerBase
    {
        private readonly ICaptcha _captcha;
        private readonly SysLoginService _sysLoginService;
        private readonly SysUserService _sysUserService;
        private readonly SysPermissionService _sysPermissionService;
        private readonly SysLogininforService _sysLogininforService;
        private readonly SysMenuService _sysMenuService;

        public SysLoginController(SysLoginService sysLoginService, ICaptcha captcha, SysUserService sysUserService, SysPermissionService sysPermissionService, SysLogininforService sysLogininforService, SysMenuService sysMenuService)
        {
            _sysLoginService = sysLoginService;
            _captcha = captcha;
            _sysUserService = sysUserService;
            _sysPermissionService = sysPermissionService;
            _sysLogininforService = sysLogininforService;
            _sysMenuService = sysMenuService;
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet("captchaImage")]
        public CaptchaImageDto GetCaptchaImageAsync()
        {
            var uuid = Guid.NewGuid();
            var captcha = _captcha.Generate(uuid.ToString());
            return new CaptchaImageDto { Img = captcha.Bytes, Uuid = uuid };
        }

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <returns></returns>
        [HttpPost("login")]
        public async Task<AjaxResult> Login(LoginDto loginBody)
        {
            AjaxResult ajax = AjaxResult.Success();
            // 生成令牌
            string token = await _sysLoginService.LoginAsync(loginBody);
            ajax.Add("token", token);
            return ajax;
        }

        /// <summary>
        /// 退出
        /// </summary>
        [HttpPost("/logout")]
        public AjaxResult Logout()
        {
            LoginUser loginUser = _sysUserService.GetLoginUser(Request);
            if (loginUser != null)
            {
                string userName = loginUser.UserName;
                // 删除用户缓存记录
                _sysUserService.DelLoginUser(loginUser.Token);
                // 记录用户退出日志
                _ = Task.Factory.StartNew(async () =>
                {
                    await _sysLogininforService.AddAsync(userName, GlobalConstants.LOGOUT, "退出成功");
                });
            }
            return AjaxResult.Success("退出成功");
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        [HttpGet("getInfo")]
        public async Task<AjaxResult> GetInfo()
        {
            var user = _sysUserService.GetLoginUser(Request).User;
            // 角色集合
            List<string> roles = await _sysPermissionService.GetRolePermissionAsync(user);
            // 权限集合
            List<string> permissions = _sysPermissionService.GetMenuPermission(user);

            AjaxResult ajax = AjaxResult.Success();
            ajax.Add("user", user);
            ajax.Add("roles", roles);
            ajax.Add("permissions", permissions);
            return ajax;
        }

        /// <summary>
        /// 获取路由信息
        /// </summary>
        [HttpGet("getRouters")]
        public async Task<AjaxResult> GetRouters()
        {
            long userId = long.Parse(_sysUserService.GetLoginUserId(Request));
            List<SysMenu> menus = await _sysMenuService.SelectMenuTreeByUserId(userId);
            var treeMenus = _sysMenuService.BuildMenus(menus);
            return AjaxResult.Success(treeMenus);
        }

    }
}
